package com.pj.point.point;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.PointerIcon;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.pj.point.R;
import com.pj.point.litepal.PointJavaBean;
import com.pj.point.receiver.AlertReceiver;
import com.pj.point.util.AlarmManagerUtil;
import com.pj.point.util.Util;
import com.pj.point.util.alert.AlertUtil;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.Calendar;

public class NewPoint extends AppCompatActivity {

    private EditText newPointTitle;
    private EditText newPointContent;
    private Button newPointTime;
    private CardView createPoint;
    private String time;
    private Calendar instance;
    private PointJavaBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_point);
        initBar();
        initView();
    }

    private void initBar() {
        ActionBar bar = getSupportActionBar();
        bar.setTitle("新增提醒");
        bar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        newPointTitle = (EditText) findViewById(R.id.new_point_title);
        newPointContent = (EditText) findViewById(R.id.new_point_content);
        newPointTime = (Button) findViewById(R.id.new_point_time);
        createPoint = (CardView) findViewById(R.id.create_point);

        isOpen();

        newPointTime.setOnClickListener(v -> {
            DatePickerDialog dialog = new DatePickerDialog(NewPoint.this);
            dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    Calendar calendar = Calendar.getInstance();
                    TimePickerDialog timePickerDialog = new TimePickerDialog(NewPoint.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            instance = Calendar.getInstance();
                            instance.set(year,month,dayOfMonth,hourOfDay,minute);
                            time = Util.getTime(instance.getTime());
                        }
                    },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),false);
                    timePickerDialog.show();
                }
            });
            dialog.show();
        });


        createPoint.setOnClickListener(v -> {
            String title = newPointTitle.getText().toString().trim();
            String content = newPointContent.getText().toString().trim();
            if (title.equals("") || content.equals("")){
                Toast.makeText(this, "请输入标题提醒内容", Toast.LENGTH_SHORT).show();
                return;
            }

            if (time==null){
                Toast.makeText(this, "请选择提醒时间", Toast.LENGTH_SHORT).show();
                return;
            }



            Calendar calendar = Calendar.getInstance();
            String pointId = Util.getTime(calendar.getTime());
            save(pointId,title,content,Util.getTime(instance.getTime()));

            Intent intent = new Intent(NewPoint.this, AlertReceiver.class);
            intent.putExtra("title",title);
            intent.putExtra("content",content);
            intent.putExtra("pointId",pointId);

            if (bean!=null){
                AlarmManagerUtil.stopRemind(NewPoint.this,intent);
            }
            AlarmManagerUtil.addAlarm(NewPoint.this,instance,intent);
            setResult(-1);
            finish();

        });
    }

    private void isOpen() {
        String id = getIntent().getStringExtra("pointId");
        if (id != null) {
            bean = LitePal.where("pointId=?", id).findFirst(PointJavaBean.class);
            newPointTitle.setText(bean.getTitle());
            newPointContent.setText(bean.getContent());
            newPointTime.setText(bean.getPointId());
        }
    }

    private void save(String pointId,String title, String content,String alertTime) {
        if (bean != null) {
             bean.setPointId(pointId);
             bean.setTitle(title);
             bean.setContent(content);
             bean.setAlertTime(alertTime);
             bean.save();
        }else{
            PointJavaBean javaBean = new PointJavaBean(pointId, title, content, alertTime);
            javaBean.save();
        }

    }
}